/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "variant.h"


bool varIsNumeric( VARIANT* pv )
{
    switch( pv->vt )
    {
    case VT_INT: case VT_I1: case VT_I2: case VT_I4: case VT_I8: case VT_R8: case VT_R4: case VT_UINT: case VT_UI1: case VT_UI2: case VT_UI4: case VT_UI8: return true;
    }
    return false;
}

bool varIsString( VARIANT* pv )
{
    return pv->vt == VT_BSTR;
}

int varAsInt( VARIANT* pv )
{
    int value = 0;

    switch( pv->vt )
    {
    case VT_I1: value = ( int ) pv->cVal; break;
    case VT_I2: value = ( int ) pv->iVal; break;
    case VT_I4:
    case VT_INT: value = ( int ) pv->intVal; break;
    case VT_I8: value = ( int ) pv->llVal; break;

    case VT_UI1: value = ( int ) pv->bVal; break;
    case VT_UI2: value = ( int ) pv->uiVal; break;
    case VT_UI4:
    case VT_UINT: value = ( int ) pv->uintVal; break;
    case VT_UI8: value = ( int ) pv->ullVal; break;

    case VT_R4: value = ( int ) pv->fltVal; break;
    case VT_R8: value = ( int ) pv->dblVal; break;

    default: break;
    }
    return value;
}

bool varAsInt( VARIANT* pv, int& value )
{
    VARIANT& v = *pv;

    switch( v.vt )
    {
    case VT_I1: value = ( int ) v.cVal; break;
    case VT_I2: value = ( int ) v.iVal; break;
    case VT_I4:
    case VT_INT: value = ( int ) v.intVal; break;
    case VT_I8: value = ( int ) v.llVal; break;

    case VT_UI1: value = ( int ) v.bVal; break;
    case VT_UI2: value = ( int ) v.uiVal; break;
    case VT_UI4:
    case VT_UINT: value = ( int ) v.uintVal; break;
    case VT_UI8: value = ( int ) v.ullVal; break;

    case VT_R4: value = ( int ) v.fltVal; break;
    case VT_R8: value = ( int ) v.dblVal; break;

    default:
        return false;
    }
    return true;
}
